package com.dao.impl;

import com.bean.BhUser;
import com.dao.BhStaduntDao;
import com.util.AliPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class BhStudentDaoImpl implements BhStaduntDao {

    QueryRunner runner=new QueryRunner();

//    @Override
//    public String Tc(int num) throws SQLException {
//        String sql = "SELECT uClass FROM bhuser WHERE uid = ?";
//        Connection conn= AliPool.getConn();
//        BhUser bu = runner.query(conn,sql,new BeanHandler<>(BhUser.class),num);
//        conn.close();
//        return  bu.uClass;
//    }

   //查询对应老师下的学生
    @Override
    public List selStu(String sum) throws SQLException {
        String sql = "SELECT * from bhuser where uSystem = 1 and uClass = ?";
        Connection conn= AliPool.getConn();
        List list=runner.query(conn,sql,new BeanListHandler<BhUser>(BhUser.class),sum);
        conn.close();
        return list;
    }

    //添加学生信息
    @Override
    public int inStu(BhUser bhUser) throws SQLException {
        String sql = "insert into bhuser (uClass,uName) values(?,?)";
        Connection conn = AliPool.getConn();

        Object pm[]=new Object[2];
        pm[0]=bhUser.uClass;
        pm[1]=bhUser.uName;

        int n=runner.update(conn,sql,pm);
        conn.close();
        return n;

    }

    //删除学生信息
    @Override
    public int delStu(int uId) throws SQLException {
        String sql = "delete from bhuser where uId = ?";
        Connection conn = AliPool.getConn();
        int i = runner.update(conn,sql,uId);
        conn.close();

        return i;
    }

    //修改学生信息
    @Override
    public int updStu(BhUser bhUser) throws SQLException {
        String sql = "update bhuser set uClass = ?,uName = ? where uid = ?";
        Connection conn = AliPool.getConn();
        Object pm[]=new Object[3];
        pm[0]=bhUser.uClass;
        pm[1]=bhUser.uName;
        pm[2]=bhUser.uID;
        int n=runner.update(conn,sql,pm);
        conn.close();
        return n;
    }

    //5页5页的查
    @Override
    public List<BhUser> getPage(String cl,int n) throws SQLException {
        int i=(n-1)*10;
        String sql = "select * from bhuser where uSystem = 1 and uClass = ? LIMIT ?,10";
        Connection conn = AliPool.getConn();
        // list 保存-- 这里的方法调用的是Users里的set方法
        List list = runner.query(conn, sql, new BeanListHandler<BhUser>(BhUser.class),cl,i);
		conn.close();

        return list;
    }

    //查询总行数
    public int hs() throws SQLException {
        String sql="select count(*) from bhuser";
        Connection conn = AliPool.getConn();
        long count=runner.query(conn, sql,new ScalarHandler<Long>());
		conn.close();
        return (int)count;
    }


}
